package com.jaya.budan.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.jaya.budan.R;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ToggleButton;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;
import com.jaya.budan.views.CommonPopWindow;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Message_Set_Activity extends BaseActivity implements View.OnClickListener, CommonPopWindow.ViewClickListener {

    private RelativeLayout back, rl_msg_set_weidu, rl_msg_set_liebiao;
    private String access_token;
    private ToggleButton tb_mes_tx_1, tb_mes_tx_2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_set);
        Intent intent = getIntent();
        access_token = intent.getStringExtra("access_token");
        back = findViewById(R.id.back);
        rl_msg_set_weidu = findViewById(R.id.rl_msg_set_weidu);
        rl_msg_set_liebiao = findViewById(R.id.rl_msg_set_liebiao);
        tb_mes_tx_1 = findViewById(R.id.tb_mes_tx_1);
        tb_mes_tx_2 = findViewById(R.id.tb_mes_tx_2);
        back.setOnClickListener(this);
        rl_msg_set_weidu.setOnClickListener(this);
        rl_msg_set_liebiao.setOnClickListener(this);
        tb_mes_tx_1.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
            @Override
            public void onToggle(boolean on) {
                if (on){
                    ToolUtils.showToast(Message_Set_Activity.this, "消息免打扰已开启");
                } else {
                    ToolUtils.showToast(Message_Set_Activity.this, "消息免打扰已关闭");
                }
            }
        });
        tb_mes_tx_2.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
            @Override
            public void onToggle(boolean on) {
                if (on){
                    ToolUtils.showToast(Message_Set_Activity.this, "悄悄查看功能已开启");
                } else {
                    ToolUtils.showToast(Message_Set_Activity.this, "悄悄查看功能已关闭");
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_msg_set_weidu:
                CommonPopWindow.newBuilder()
                        .setView(R.layout.pop_geren_fans_cancle)
                        .setAnimationStyle(R.style.AnimUp)
                        .setBackgroundDrawable(new BitmapDrawable())
                        .setSize(0, 600)
                        .setViewOnClickListener(Message_Set_Activity.this)
                        .setBackgroundDarkEnable(true)
                        .setBackgroundAlpha(0.7f)
                        .build(Message_Set_Activity.this)
                        .showAsCenter(v);
                break;
            case R.id.rl_msg_set_liebiao:
                CommonPopWindow.newBuilder()
                        .setView(R.layout.pop_geren_fans_cancle_2)
                        .setAnimationStyle(R.style.AnimUp)
                        .setBackgroundDrawable(new BitmapDrawable())
                        .setSize(0, 600)
                        .setViewOnClickListener(Message_Set_Activity.this)
                        .setBackgroundDarkEnable(true)
                        .setBackgroundAlpha(0.7f)
                        .build(Message_Set_Activity.this)
                        .showAsCenter(v);
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postData() {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.gr_xyf_Url + access_token + "&limit=10", null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("xinyun", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                    } else {
                        ToolUtils.showToast(Message_Set_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(Message_Set_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    @SuppressLint({"NonConstantResourceId", "SetTextI18n"})
    @Override
    public void getChildView(final PopupWindow mPopupWindow, View view, int mLayoutResId) {
        switch (mLayoutResId) {
            case R.layout.pop_geren_fans_cancle:
                TextView tv_cancle = view.findViewById(R.id.tv_cancle);
                TextView tv_geren_fans_cancle = view.findViewById(R.id.tv_geren_fans_cancle);
                TextView img_guanbi = view.findViewById(R.id.img_guanbi);
                tv_geren_fans_cancle.setText("确定忽略未读消息吗？");
                tv_cancle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                    }
                });
                img_guanbi.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                    }
                });
                break;
            case R.layout.pop_geren_fans_cancle_2:
                TextView tv_cancle1 = view.findViewById(R.id.tv_cancle);
                TextView tv_geren_fans_cancle1 = view.findViewById(R.id.tv_geren_fans_cancle);
                TextView img_guanbi1 = view.findViewById(R.id.img_guanbi);
                tv_geren_fans_cancle1.setText("确定清空聊天的对话列表吗？\n" +
                        "（不会删除聊天记录）");
                tv_cancle1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                    }
                });
                img_guanbi1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                    }
                });
                break;
            default:
                break;
        }
    }
}
